package com.zeng.jiudian.servlet.nav;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.NavModel;
import com.zeng.jiudian.service.NavService;
import com.zeng.jiudian.service.impl.NavServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 16:36
 */
@WebServlet("/back/nav/add")
public class NavAddServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String navName = req.getParameter("navName");
        String navHref = req.getParameter("navHref");
        String sortString = req.getParameter("sort");
        int sort = (sortString == null || "".equals(sortString) ? 0 : Integer.parseInt(sortString));
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");

        NavModel navModel = new NavModel();
        navModel.setNavName(navName);
        navModel.setNavHref(navHref);
        navModel.setSort(sort);
        navModel.setIsShow(isShow == null ? 0 : Integer.parseInt(isShow));
        navModel.setRecommend(isShow == null ? 0 : Integer.parseInt(recommend));

        NavService navService = new NavServiceImpl();
        int count = navService.add(navModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
